package com.takeoutsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.takeoutsystem.bean.Cabinet;
import com.takeoutsystem.bean.StorageRemain;
import com.takeoutsystem.bean.StorageRemainShow;
import com.takeoutsystem.event.publisher.SendMailPublisher;
import com.takeoutsystem.service.CabinetService;
import com.takeoutsystem.service.RemainTakeoutService;
import com.takeoutsystem.service.RemainTakeoutShowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 外卖员相关功能控制类
 */
@RestController
public class SenderController {
    @Autowired
    CabinetService cabinetService;
    @Autowired
    RemainTakeoutService remainService;
    @Autowired
    RemainTakeoutShowService remainShowService;
    @Autowired
    SendMailPublisher publisher;

    /**
     * 获取所有空柜信息
     * @return 空柜信息列表
     */
    @GetMapping("/listFreeCab")
    public List<Cabinet> getAllFreeCabs(){
        QueryWrapper<Cabinet> wrapper=new QueryWrapper<>();
        wrapper.eq("state_code",1);
        return cabinetService.list(wrapper);
    }

    /**
     * 保存外卖员提交的存柜信息
     * @param remain 存柜信息
     */
    @GetMapping("/saveOrder")
    @Transactional
    public void saveOrder(StorageRemain remain){
        remainService.saveOrder(remain);
        Cabinet cabinet=cabinetService.getById(remain.getCabId());
        cabinet.setStateCode(2);
        cabinetService.updateById(cabinet);
    }
    /**
     * 发送取餐提醒短信
     */
    @GetMapping("/sendMail")
    public void sendMail(@RequestParam("cabId") String cabId){
        StorageRemainShow info=remainShowService.getById(cabId);
        publisher.sendGetOrderMail(info);
    }
}
